requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        list: "manager/selectRoleList.do",
        editRole: "manager/updateRole.do",
        addRole: "manager/insertRole.do",
        colseopen: "manager/updateAdminsEnable.do",
    }
    var listid = "";
    var delectData = {
        ids: listid
    };

    var id;

    //增加角色
    var addRole = function (url) {
        var data = {
            roleName: $('#name').val(),
            description: $('#description').val(),
            type: $('#auth option:selected').val()
        };
        if (!data.roleName) {
            Constant.fn.Message('I', '请输入职位名称');
            return;
        }
        if (!data.type) {
            Constant.fn.Message('I', '请选择权限');
            return;
        }
        site.send({
            url: url,
            data: data,
            callback: function (datas) {
                datas = JSON.parse(datas)
                if (datas.type == 1) {
                    Constant.fn.Message('T', datas.msg);
                } else {
                    Constant.fn.Message('F', datas.msg);
                }
                $('.add_winbox').toggle();
                pjaxReload();//刷新当前页面.
            }
        })
    }
    //编辑角色
    var editRole = function (url) {
        //如果是编辑的话，获取弹窗里面的参数
        var data = {
            roleName: $('#name').val(),
            description: $('#description').val(),
            id: $('#id').val(),
            type: $("#auth").val()
        };
        site.send({
            url: url,
            data: data,
            callback: function (datas) {
                datas = JSON.parse(datas)
                if (datas.type == 1) {
                    Constant.fn.Message('T', datas.msg);
                    pjaxReload();//刷新当前页面.
                }
                else {
                    Constant.fn.Message('F', datas.msg);
                }
                $('.add_winBox').toggle();

            }
        })
    }
    //为所有的弹窗绑定事件
    var initWinBoxAciton = function (attr) {
        $("#submit").on('click', function () {
            if (attr == "editRole") {
                editRole(url.editRole);
            }
            if (attr == "addRole") {
                addRole(url.addRole);
            }
        })

    }
    //为所有角色初始化按钮
    var initBtnAction = function () {
        //拿到所有的复选框，当有复选框没选上的时候,取消全选
        $("input:checkbox[name='ch_id']").on('click', function () {
            var checkboxs = $("input:checkbox[name='ch_id']");
            var sum = 0;
            var length = checkboxs.length;
            $.each(checkboxs, function (i, checkbox) {
                if ($(checkbox).prop("checked")) {
                    sum++;
                }
            })
            if (sum == length) {
                $('#checkboxAll').prop("checked", true);
            } else {
                $('#checkboxAll').prop("checked", false);
            }
        })
        //全选和全不选
        $('#checkboxAll').on('click', function () {
            $("input[name='ch_id']").prop("checked", $(this).prop("checked"));
        })
        //增加角色
        $('#addTop').on('click', function () {
            var r = {
                role_name: $(this).data('name'),
                description: $(this).data('description')
            }
            var source = $("#role_window_tpl").html();
            var template = Handlebars.compile(source);
            $('#role_window').html(template(r));
            $('.add_winbox').toggle();
            initWinBoxAciton("addRole");
        })

        //编辑角色
        $('button[data-attr=editRole]').on('click', function () {
            //获取内容填充到弹窗上
            var r = {
                id: $(this).data('id'),
                role_name: $(this).data('name'),
                description: $(this).data('description')
            }
            var source = $("#role_window_tpl").html();
            var template = Handlebars.compile(source);
            $('#role_window').html(template(r));
            $(".headerSpan").text("编辑职位");
            $('#auth option[value=' + $(this).data('type') + ']').prop('selected', true);
            $('.add_winbox').toggle();
            ;
            initWinBoxAciton("editRole");
        })

        //删除角色
        $('button[data-attr=deleteRight]').on('click', function () {
            var name = $(this).data('name')
            id = $(this).data('id');
            //delectRoleSend(data);
            $(".delete_winBox").show();
        })

        $("#sureDelete").on('click', function () {
            delectRoleSend()
        })

        // 删除角色调用
        var delectRoleSend = function (data) {

            site.send({
                url: "manager/deleteRoleById.do",
                data: {
                    ids: id
                },
                callback: function (data) {
                    data = JSON.parse(data)
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);
                        //$('.winBox').toggle();
                        pjaxReload();//刷新当前页面.
                    }
                    else {
                        Constant.fn.Message('F', data.msg);
                        // $('.winBox').toggle();
                    }

                }
            })
        }

        //权限明细
        $('button[data-attr=selectRights]').on('click', function () {
            //将角色信息带过去权限页，保存权限的时候并可以修改角色信息
            var id = $(this).data('id');
            // var roleName=$(this).data('name');
            // var description=$(this).data('description');
            //+"&roleName="+roleName+"&description="+description
            pjaxGo("OperaManager/limsdetail/lims_detail.html?id=" + id + "&t=1");
        })

        // 批量操作

        $(".tabHint").on('change', function () {
            var val = $(this).find("option:selected").val();
            // 批量删除
            if (val == 1) {
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;//计数
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        if (count < 2) {
                            listid = inp[i].value;
                        } else {
                            listid += ("," + inp[i].value);
                        }
                        //delectData.ids = listid;
                        id = listid;

                    }
                }
                if (count != 0) {

                    showDelect('delect');
                } else {
                    Constant.fn.Message('I', "未选择");

                }


                $(".tabHint").val('-1')
            } // 批量禁用
            if (val == 2) {
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;//计数
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        if (count < 2) {
                            listid = inp[i].value;
                        } else {
                            listid += ("," + inp[i].value);
                        }
                        delectData.ids = listid;
                    }
                }
                if (count != 0) {
                    showDelect('close');
                } else {
                    Constant.fn.Message('I', "未选择");
                }

                $(".tabHint").val('-1')
            }
            //批量开启
            if (val == 3) {
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;//计数
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        if (count < 2) {
                            listid = inp[i].value;
                        } else {
                            listid += ("," + inp[i].value);
                        }
                        delectData.ids = listid;
                    }
                }
                if (count != 0) {
                    showDelect('open');
                } else {
                    Constant.fn.Message('I', "未选择");
                }
                $(".tabHint").val('-1')

            }
        })

        // 禁用、启用、删除弹框
        function showDelect(status) {

            $(".winBoxDelect").toggle();
            $(".winBoxDelect").css("z-index", 0);
            $(".winBoxDelect_gray").toggle();
            if (status == 'delect') {
                $(".winBoxDelect_title").text("删除职位")
                $(".winBoxDelect_content").text("您确定要删除所选的职位吗")
                confirmToSubmit(status);

            }
            if (status == 'close') {
                $(".winBoxDelect_title").text("禁用职位")
                $(".winBoxDelect_content").text("您确定要禁用所选的职位吗")
                confirmToSubmit(0);
            }
            if (status == 'open') {
                $(".winBoxDelect_title").text("开启职位")
                $(".winBoxDelect_content").text("您确定要开启所选的职位吗")
                confirmToSubmit(1);
            }
        }

        // 隐藏弹框
        $(".winBoxDelect_hidePop").on('click', function () {
            $(".winBoxDelect").toggle();
            $(".winBoxDelect_gray").toggle();
        })
        // 删除提交
        var confirmToSubmit = function (status) {
            $(".delectBtn").click(function () {

                // 判断点击删除
                if (status == 'delect') {
                    delectRoleSend(delectData);
                }
                // 判断点击的是禁用或开启
                if (status == 0 || status == 1) {
                    site.send({
                        url: url.colseopen,
                        //async:false
                        data: {
                            locked: status,
                            ids: delectData.ids,
                            reason: $(".delect_reason").val()
                        },
                        callback: function (data) {
                            data = JSON.parse(data);
                            Constant.fn.Message("F", data.msg)
                            if (data.type == 1) {
                                $(".winBoxDelect").toggle();
                                $(".winBoxDelect_gray").toggle();
                                Constant.fn.Message('T', data.msg);
                                pjaxReload()
                            }
                        }
                    })
                }
            })


        }
    }
    //显示所有的角色
    site.send({
        url: url.list,
        //async:false
        data: {},
        callback: function (data) {
            var data = JSON.parse(data);
            var source = $("#role_tpl").html();
            var template = Handlebars.compile(source);
            var html = template(data.content);
            $('#body').append(html);
            initBtnAction();
        }
    })
})